package com.avic.rong.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * ScreenUtils
 * Created by hanj on 14-9-25.
 */
public class ScreenUtils {
    private static int screenW;
    private static int screenH;

    public static int getScreenW(Activity mActivity) {
        if (screenW == 0) {
            initScreen(mActivity);
        }
        return screenW;
    }

    public static int getScreenH(Activity mActivity) {
        if (screenH == 0) {
            initScreen(mActivity);
        }
        return screenH;
    }

    private static void initScreen(Activity mActivity) {
        DisplayMetrics metric = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        screenW = metric.widthPixels;
        screenH = metric.heightPixels;
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);//将当前窗口的信息放在DisplayMetrics类中
        return outMetrics.widthPixels;
    }/**
     * 获取屏幕宽度
     */
    public static int getScreenWidthPixels(Activity context) {
        DisplayMetrics metric = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }
    public static int getScreenHeight(Context ctx) {
        Resources re = ctx.getResources();
        return re.getDisplayMetrics().heightPixels;
    }
    /***
     * 获取屏幕宽高（除去通知栏）
     *
     * @param mActivity
     * @return
     */
    public static int getScreenHeightWithoutStatusBar(Context mActivity) {
        return getScreenHeight(mActivity) - getStatusBarHeight(mActivity);
    }

    public static int getStatusBarHeight(Context ctx) {
        int statusBarHeight = 50;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            Field field = clazz.getField("status_bar_height");
            int height = string2int(field.get(object).toString());
            statusBarHeight = ctx.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }
    /**
     * String 类型 转 int 类型 数字
     *
     * @param numbler
     * @return
     */
    public static int string2int(String numbler) {
        int n = 0;
        if (TextUtils.isEmpty(numbler)) {
            return n;
        }
        try {
            n = Integer.parseInt(numbler);
        } catch (NumberFormatException e) {

            e.printStackTrace();
            n = 0;
        }
        return n;
    }
    /**
     * 将dp值转换为px值
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    /**
     * 将px值转换为dp值
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
    /**
     * 将px值转换为sp值
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


}
